package com.yyge.util;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yyge.common.constans.RedisConstans;
import com.yyge.entity.SysDictData;
import com.yyge.service.SysDictDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.awt.*;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 字典缓存工具类
 */
@Component
public class DicUtil {

    @Autowired
    private SysDictDataService service;

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * Sprinq Bean生命周期回调函数
     */
    @PostConstruct
    public  void lifeCirleInit(){
        this.removeAllRedisDictData();
        this.loadAllDictDataToRedis();
    }

    /**
     * 加载sys_dict_data所有的数据到redis缓存中
     * @return boolean
     */

    public boolean loadAllDictDataToRedis(){
        List<SysDictData> list=this.service.list();
        Map<String, List<SysDictData>> collect = list.stream().collect(Collectors.groupingBy(SysDictData::getDictType));
        for (String key:collect.keySet()){
            String  redisKey = RedisConstans.getRedisKey(RedisConstans.DICT_DATA_KEY,key);
            this.redisTemplate.opsForValue().set(redisKey,collect.get(key));
        }
       return true;
    }

    /**
     * 移出redis中所有的sys_dict_data缓存
     */
    public  void removeAllRedisDictData(){
        String redisKey=RedisConstans.getRedisKey(RedisConstans.DICT_DATA_KEY,"*");
        Set keys = this.redisTemplate.keys(redisKey);
        this.redisTemplate.delete(keys);
    }

    /**
     * 根据dictType查询缓存
     * @param dictType
     * @return
     */
    public List<SysDictData> findByDictType(String dictType){
       String redisKey=RedisConstans.getRedisKey(RedisConstans.DICT_DATA_KEY,dictType);
        Object redisValue = this.redisTemplate.opsForValue().get(redisKey);
        if(Objects.isNull((redisValue))){

            return null;
        }
        return (List<SysDictData>) redisValue;
    }

    /**
     * 刷新redis的某个类型的缓存数据
     * @param dictType
     * @return
     */
    public void refreshCacheByDictType(String dictType){
        String redisKey=RedisConstans.getRedisKey(RedisConstans.DICT_DATA_KEY,dictType);
        LambdaQueryWrapper<SysDictData> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(SysDictData::getDictType,dictType);
        queryWrapper.eq(SysDictData::getStatus,"0");

        List<SysDictData> list = this.service.list(queryWrapper);
        //没有数据，就要移出缓存
        if(list.size()==0){
            this.redisTemplate.delete(redisKey);
        }else{
            //新增缓存
            this.redisTemplate.opsForValue().set(redisKey,list);
        }
    }

    /**
     * 从缓存中移出某个指定的键
     * @param dictType
     */
    public void removeByDictType(String dictType){
        String redisKey=RedisConstans.getRedisKey(RedisConstans.DICT_DATA_KEY,dictType);
        this.redisTemplate.delete(redisKey);
    }
}
